<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="60%" class="channel-dialog">
    <div class="form-container">
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="120px"
        v-loading="formLoading"
        class="channel-form"
      >
        <!-- 基本信息区域 -->
        <div class="form-section">
          <div class="section-title">
            <Icon icon="ep:info-filled" class="section-icon" />
            <span>基本信息</span>
          </div>

          <el-form-item label="渠道名称" prop="name" class="form-item">
            <el-input
              v-model="formData.name"
              placeholder="请输入渠道名称"
              class="form-input"
              clearable
              maxlength="50"
            >
              <template #prefix>
                <Icon icon="ep:connection" class="input-icon" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="渠道编码" prop="code" class="form-item">
            <el-input
              v-model="formData.code"
              placeholder="请输入渠道编码"
              class="form-input"
              clearable
              maxlength="30"
            >
              <template #prefix>
                <Icon icon="ep:key" class="input-icon" />
              </template>
            </el-input>
            <div class="input-tip">
              <Icon icon="ep:info-filled" class="tip-icon" />
              <span>渠道编码用于系统内部识别，建议使用英文字母和数字</span>
            </div>
          </el-form-item>

          <el-form-item label="呼叫模式" prop="phoneMode" class="form-item">
            <el-select
              v-model="formData.phoneMode"
              placeholder="请选择呼叫模式"
              clearable
              class="form-select"
            >
              <el-option label="点呼" value="p2p">
                <div class="option-item">
                  <Icon icon="ep:phone" class="option-icon" />
                  <div class="option-content">
                    <span class="option-label">点呼</span>
                    <span class="option-desc">直接拨打目标号码</span>
                  </div>
                </div>
              </el-option>
              <el-option label="双向回拨" value="recall">
                <div class="option-item">
                  <Icon icon="ep:refresh" class="option-icon" />
                  <div class="option-content">
                    <span class="option-label">双向回拨</span>
                    <span class="option-desc">系统先呼叫坐席，再呼叫客户</span>
                  </div>
                </div>
              </el-option>
              <el-option label="无线话机" value="box">
                <div class="option-item">
                  <Icon icon="ep:box" class="option-icon" />
                  <div class="option-content">
                    <span class="option-label">无线话机</span>
                    <span class="option-desc">使用无线话机设备</span>
                  </div>
                </div>
              </el-option>
              <el-option label="手机sim卡外呼" value="sim">
                <div class="option-item">
                  <Icon icon="ep:mobile-phone" class="option-icon" />
                  <div class="option-content">
                    <span class="option-label">手机sim卡外呼</span>
                    <span class="option-desc">通过手机SIM卡进行外呼</span>
                  </div>
                </div>
              </el-option>
            </el-select>
          </el-form-item>
        </div>

        <!-- API配置区域 -->
        <div class="form-section">
          <div class="section-title">
            <Icon icon="ep:key" class="section-icon" />
            <span>API配置</span>
          </div>

          <el-form-item label="appId" prop="appId" class="form-item">
            <el-input
              v-model="formData.appId"
              placeholder="请输入appId"
              class="form-input"
              clearable
            >
              <template #prefix>
                <Icon icon="ep:key" class="input-icon" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="appKey" prop="appKey" class="form-item">
            <el-input
              v-model="formData.appKey"
              placeholder="请输入appKey"
              class="form-input"
              clearable
            >
              <template #prefix>
                <Icon icon="ep:lock" class="input-icon" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="appSecret" prop="appSecret" class="form-item">
            <el-input
              v-model="formData.appSecret"
              placeholder="请输入appSecret"
              class="form-input"
              show-password
              clearable
            >
              <template #prefix>
                <Icon icon="ep:hide" class="input-icon" />
              </template>
            </el-input>
            <div class="input-tip warning">
              <Icon icon="ep:warning" class="tip-icon" />
              <span>appSecret是敏感信息，请妥善保管</span>
            </div>
          </el-form-item>
        </div>

        <!-- 回调配置区域 -->
        <div class="form-section">
          <div class="section-title">
            <Icon icon="ep:link" class="section-icon" />
            <span>回调配置</span>
          </div>

          <el-form-item label="话单推送地址" prop="notifyUrl" class="form-item">
            <el-input
              v-model="formData.notifyUrl"
              placeholder="请输入话单推送地址"
              class="form-input"
              clearable
            >
              <template #prefix>
                <Icon icon="ep:link" class="input-icon" />
              </template>
            </el-input>
            <div class="input-tip">
              <Icon icon="ep:info-filled" class="tip-icon" />
              <span>系统将向此地址推送通话记录，请确保地址可访问</span>
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button class="cancel-btn" @click="dialogVisible = false">
          <Icon icon="ep:close" class="btn-icon" />
          取消
        </el-button>
        <el-button
          type="primary"
          class="submit-btn"
          :disabled="formLoading"
          :loading="formLoading"
          @click="submitForm"
        >
          <Icon icon="ep:check" class="btn-icon" v-if="!formLoading" />
          {{ formLoading ? '提交中...' : '确定' }}
        </el-button>
      </div>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import * as ChannelApi from '@/api/call/channel'

/** 外呼渠道 表单 */
defineOptions({ name: 'ChannelForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  name: undefined,
  code: undefined,
  phoneMode: undefined,
  appId: undefined,
  appKey: undefined,
  appSecret: undefined,
  notifyUrl: undefined
})
const formRules = reactive({
  name: [{ required: true, message: '渠道名称不能为空', trigger: 'blur' }],
  code: [{ required: true, message: '渠道编码不能为空', trigger: 'blur' }],
  phoneMode: [{ required: true, message: '请选择呼叫模式', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ChannelApi.getChannel(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ChannelApi.ChannelVO
    if (formType.value === 'create') {
      await ChannelApi.createChannel(data)
      message.success(t('common.createSuccess'))
    } else {
      await ChannelApi.updateChannel(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: undefined,
    code: undefined,
    phoneMode: undefined,
    appId: undefined,
    appKey: undefined,
    appSecret: undefined,
    notifyUrl: undefined
  }
  formRef.value?.resetFields()
}
</script>

<style lang="scss" scoped>
// 对话框样式
.channel-dialog {
  :deep(.el-dialog) {
    border-radius: 16px;
    overflow: hidden;

    .el-dialog__header {
      background: linear-gradient(135deg, #97a5e2 0%, #7db5db 100%);
      color: white;
      padding: 20px 24px;

      .el-dialog__title {
        color: white;
        font-size: 18px;
        font-weight: 600;
      }

      .el-dialog__headerbtn {
        .el-dialog__close {
          color: rgba(255, 255, 255, 0.8);
          font-size: 18px;

          &:hover {
            color: white;
          }
        }
      }
    }

    .el-dialog__body {
      padding: 0;
    }

    .el-dialog__footer {
      padding: 0;
      border-top: 1px solid #f1f5f9;
    }
  }
}

// 表单容器
.form-container {
  padding: 24px;
  max-height: 70vh;
  overflow-y: auto;
}

.channel-form {
  .form-section {
    margin-bottom: 32px;

    &:last-child {
      margin-bottom: 0;
    }
  }
}

// 区域标题
.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 2px solid #f1f5f9;

  .section-icon {
    color: #667eea;
    font-size: 18px;
  }

  span {
    font-size: 16px;
    font-weight: 600;
    color: #374151;
  }
}

// 表单项
.form-item {
  margin-bottom: 24px;

  :deep(.el-form-item__label) {
    font-weight: 500;
    color: #374151;
    line-height: 1.6;
  }

  :deep(.el-form-item__content) {
    line-height: normal;
  }
}

// 输入框样式
.form-input {
  :deep(.el-input__wrapper) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;

    &:hover {
      border-color: #d1d5db;
    }

    &.is-focus {
      border-color: #667eea;
      box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }
  }

  .input-icon {
    color: #9ca3af;
    font-size: 16px;
  }
}

// 选择框样式
.form-select {
  width: 100%;

  :deep(.el-select__wrapper) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;

    &:hover {
      border-color: #d1d5db;
    }

    &.is-focused {
      border-color: #667eea;
      box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }
  }
}

// 选项样式
.option-item {
  display: flex;
  align-items: center;
  gap: 12px;

  .option-icon {
    font-size: 16px;
    color: #6b7280;
    flex-shrink: 0;
  }

  .option-content {
    display: flex;
    flex-direction: column;
    gap: 2px;

    .option-label {
      font-weight: 500;
      color: #374151;
    }

    .option-desc {
      font-size: 12px;
      color: #6b7280;
    }
  }
}

// 输入提示
.input-tip {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-top: 8px;
  padding: 12px;
  background: #f0f9ff;
  border: 1px solid #e0f2fe;
  border-radius: 8px;

  .tip-icon {
    color: #0ea5e9;
    font-size: 16px;
    margin-top: 2px;
    flex-shrink: 0;
  }

  span {
    font-size: 13px;
    color: #0369a1;
    line-height: 1.4;
  }

  &.warning {
    background: #fef3c7;
    border-color: #fbbf24;

    .tip-icon {
      color: #f59e0b;
    }

    span {
      color: #92400e;
    }
  }
}

// 对话框底部
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px 24px;
  background: #f8fafc;
}

.cancel-btn {
  padding: 10px 20px;
  border-radius: 8px;
  border: 2px solid #e5e7eb;
  background: white;
  color: #6b7280;
  font-weight: 500;
  transition: all 0.3s ease;

  .btn-icon {
    margin-right: 6px;
    font-size: 14px;
  }

  &:hover {
    border-color: #d1d5db;
    color: #374151;
  }
}

.submit-btn {
  padding: 10px 24px;
  border-radius: 8px;
  background: linear-gradient(135deg, #97a5e2 0%, #7db5db 100%);
  border: none;
  color: white;
  font-weight: 500;
  transition: all 0.3s ease;

  .btn-icon {
    margin-right: 6px;
    font-size: 14px;
  }

  &:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
  }

  &:disabled {
    opacity: 0.7;
    cursor: not-allowed;
  }
}

// H5 响应式设计
@media (max-width: 768px) {
  .channel-dialog {
    :deep(.el-dialog) {
      width: 95% !important;
      margin: 20px auto;

      .el-dialog__header {
        padding: 16px 20px;

        .el-dialog__title {
          font-size: 16px;
        }
      }
    }
  }

  .form-container {
    padding: 20px;
    max-height: 60vh;
  }

  .channel-form {
    :deep(.el-form-item__label) {
      font-size: 14px;
    }
  }

  .section-title {
    span {
      font-size: 15px;
    }
  }

  .option-content {
    .option-desc {
      display: none;
    }
  }

  .dialog-footer {
    padding: 16px 20px;

    .cancel-btn,
    .submit-btn {
      padding: 12px 16px;
      font-size: 14px;
    }
  }
}

@media (max-width: 480px) {
  .channel-dialog {
    :deep(.el-dialog) {
      width: 100% !important;
      margin: 0;
      height: 100vh;
      border-radius: 0;
    }
  }

  .form-container {
    padding: 16px;
    max-height: calc(100vh - 140px);
  }

  .form-section {
    margin-bottom: 24px;
  }

  .form-item {
    margin-bottom: 20px;
  }

  .dialog-footer {
    flex-direction: column;
    gap: 8px;

    .cancel-btn,
    .submit-btn {
      width: 100%;
      justify-content: center;
    }
  }
}

// 深色模式支持
@media (prefers-color-scheme: dark) {
  .channel-dialog {
    :deep(.el-dialog) {
      background: #1e293b;

      .el-dialog__body {
        background: #1e293b;
      }
    }
  }

  .form-container {
    background: #1e293b;
  }

  .section-title {
    border-color: #334155;

    span {
      color: #f1f5f9;
    }
  }

  .form-item {
    :deep(.el-form-item__label) {
      color: #e2e8f0;
    }
  }

  .option-content {
    .option-label {
      color: #f1f5f9;
    }

    .option-desc {
      color: #94a3b8;
    }
  }

  .input-tip {
    background: #1e3a8a;
    border-color: #1e40af;

    span {
      color: #93c5fd;
    }

    &.warning {
      background: #451a03;
      border-color: #92400e;

      span {
        color: #fdba74;
      }
    }
  }

  .dialog-footer {
    background: #0f172a;
    border-color: #334155;
  }
}
</style>
